package com.study.thread;

import java.util.concurrent.FutureTask;

public class CallableTest
{
    public static void main(String[] args) throws InterruptedException
    {
        FutureTask<Integer> task = new FutureTask<>(() -> {
            int i = 0;
            for (i = 0; i < 10; i++)
            {
                System.out.println(Thread.currentThread().getName() + "=======" + i);
            }
            return i;
        });
        for (int i = 0; i < 10; i++)
        {
            System.out.println(Thread.currentThread().getName() + "=======" + i);
            if (i == 0)
            {
                Thread thread = new Thread(task, "我是fu线程");
                thread.start();
            }
        }
        /*new Thread(() -> {
            for (int i = 0; i < 10; i++)
            {
                System.out.println(Thread.currentThread().getName() + "-------->" + i);
                if (i == 0)
                {
                    Thread thread = new Thread(task, "我是fu线程");
                    thread.start();
                }
            }
        }, "父线程").start();*/
        //得到返回值，注意这个必须要有显示抛出异常
        try
        {
            System.out.println("子线程的返回值" + task.get());
        } catch (Exception e)
        {
            e.printStackTrace();
        }
    }
}
